﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StudentReport.Logic;
using System.Windows.Forms.DataVisualization.Charting;

namespace StudentReport
{
    public partial class ClassResults : Form
    {
        private string filePath = string.Empty;
        private string className = string.Empty;
        private string teacher = string.Empty;

        public ClassResults(string filePath, string className, string teacher)
        {
            this.filePath = filePath;
            this.className = className;
            this.teacher = teacher;

            InitializeComponent();

            ClassLabel.Text = className;
            TeacherLabel.Text = teacher;
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void UnitTestChartButton_Click(object sender, EventArgs e)
        {
            if (SubjectUnitTextBox.Text.Equals("Subject"))
                SubjectUnitTextBox.Text = String.Empty;


            string subject = SubjectUnitTextBox.Text;
            StudentUnitProgressChart.Titles.Clear();

            try
            {
                ExcelAccess xlAccess = new ExcelAccess(filePath);

                DataTable table = xlAccess.SelectAverageResults("Unit", subject);



                Point[] points = new Point[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                 
                    points[i] = new Point(i + 1, Convert.ToInt32(Convert.ToDouble(table.Rows[i][0].ToString())));

                }
                StudentUnitProgressChart.Titles.Add("Unit Test Marks Distribution Of " + subject + " Subject");
                StudentUnitProgressChart.ChartAreas[0].AxisX.Title = "Unit Test";
                StudentUnitProgressChart.Legends.Add(subject);

                Series series = new Series(subject);
                series.ChartType = SeriesChartType.Bar;



                series.Points.DataBind(points, "X", "Y", String.Empty);
                StudentUnitProgressChart.Series.Add(series);
                
                
           
            }
            catch (Exception) { }
        }

        private void SubjectTextBox_Click(object sender, EventArgs e)
        {
            SubjectUnitTextBox.Text = String.Empty;
            SubjectUnitTextBox.ForeColor = Color.Black;
        }

        private void ShowTermProgressButton_Click(object sender, EventArgs e)
        {
            if (SubjectTermTextBox.Text.Equals("Subject"))
                SubjectTermTextBox.Text = String.Empty;


            string subject = SubjectTermTextBox.Text;
            StudentTermProgressChart.Titles.Clear();

            try
            {
                ExcelAccess xlAccess = new ExcelAccess(filePath);

                DataTable table = xlAccess.SelectAverageResults("Term", subject);



                Point[] points = new Point[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {

                    points[i] = new Point(i + 1, Convert.ToInt32(Convert.ToDouble(table.Rows[i][0].ToString())));

                }
                StudentTermProgressChart.Titles.Add("Term Test Marks Distribution Of " + subject + " Subject");
                StudentTermProgressChart.ChartAreas[0].AxisX.Title = "Term Test";
                StudentTermProgressChart.Legends.Add(subject);

                Series series = new Series(subject);
                series.ChartType = SeriesChartType.Bar;



                series.Points.DataBind(points, "X", "Y", String.Empty);
                StudentTermProgressChart.Series.Add(series);



            }
            catch (Exception) { }
        }

        private void SubjectTermTextBox_Click(object sender, EventArgs e)
        {
            SubjectTermTextBox.Text = String.Empty;
            SubjectTermTextBox.ForeColor = Color.Black;
        }

        private void SubjectTermTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString().Equals("Return"))
                ShowTermProgressButton_Click(null, null);
        }

        private void SubjectUnitTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString().Equals("Return"))
                UnitTestChartButton_Click(null, null);
        }

        private void PrintTermChartButton_Click(object sender, EventArgs e)
        {
            StudentTermProgressChart.Printing.PrintDocument.Print();
        }

        private void PrintUnitChartButton_Click(object sender, EventArgs e)
        {
            StudentUnitProgressChart.Printing.PrintDocument.Print();
        }

        private void btnClearUnitSubject_Click(object sender, EventArgs e)
        {
            StudentUnitProgressChart.Titles.Clear();
            StudentUnitProgressChart.Series.Clear();
            StudentUnitProgressChart.Legends.Clear();
        }

        private void btnClearTermSubjects_Click(object sender, EventArgs e)
        {
            StudentTermProgressChart.Titles.Clear();
            StudentTermProgressChart.Series.Clear();
            StudentTermProgressChart.Legends.Clear();
        }

    }
}
